{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Tce3stUlHN0L"
      },
      "source": [
        "##### Copyright 2020 The TensorFlow Authors."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "tuOe1ymfHZPu"
      },
      "outputs": [],
      "source": [
        "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
        "# you may not use this file except in compliance with the License.\n",
        "# You may obtain a copy of the License at\n",
        "#\n",
        "# https://www.apache.org/licenses/LICENSE-2.0\n",
        "#\n",
        "# Unless required by applicable law or agreed to in writing, software\n",
        "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
        "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
        "# See the License for the specific language governing permissions and\n",
        "# limitations under the License."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "qFdPvlXBOdUN"
      },
      "source": [
        "# Introduction to Variables"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "MfBg1C5NB3X0"
      },
      "source": [
        "<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
        "  <td>\n",
        "    <a target=\"_blank\" href=\"https://www.tensorflow.org/guide/variable\"><img src=\"https://www.tensorflow.org/images/tf_logo_32px.png\" />View on TensorFlow.org</a>\n",
        "  </td>\n",
        "  <td>\n",
        "    <a target=\"_blank\" href=\"https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/guide/variable.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
        "  </td>\n",
        "  <td>\n",
        "    <a target=\"_blank\" href=\"https://github.com/tensorflow/docs/blob/master/site/en/guide/variable.ipynb\"><img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a>\n",
        "  </td>\n",
        "  <td>\n",
        "    <a href=\"https://storage.googleapis.com/tensorflow_docs/docs/site/en/guide/variable.ipynb\"><img src=\"https://www.tensorflow.org/images/download_logo_32px.png\" />Download notebook</a>\n",
        "  </td>\n",
        "</table>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "AKhB9CMxndDs"
      },
      "source": [
        "A TensorFlow **variable** is the recommended way to represent shared, persistent state your program manipulates. This guide covers how to create, update, and manage instances of `tf.Variable` in TensorFlow.\n",
        "\n",
        "Variables are created and tracked via the `tf.Variable` class. A `tf.Variable` represents a tensor whose value can be changed by running ops on it.  Specific ops allow you to read and modify the values of this tensor. Higher level libraries like `tf.keras` use `tf.Variable` to store model parameters. "
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "xZoJJ4vdvTrD"
      },
      "source": [
        "## Setup\n",
        "\n",
        "This notebook discusses variable placement.  If you want to see on what device your variables are placed, uncomment this line."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "7tUZJk7lDiGo"
      },
      "outputs": [],
      "source": [
        "import tensorflow as tf\n",
        "\n",
        "# Uncomment to see where your variables get placed (see below)\n",
        "# tf.debugging.set_log_device_placement(True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "vORGXDarogWm"
      },
      "source": [
        "## Create a variable\n",
        "\n",
        "To create a variable, provide an initial value.  The `tf.Variable` will have the same `dtype` as the initialization value."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "dsYXSqleojj7"
      },
      "outputs": [],
      "source": [
        "my_tensor = tf.constant([[1.0, 2.0], [3.0, 4.0]])\n",
        "my_variable = tf.Variable(my_tensor)\n",
        "\n",
        "# Variables can be all kinds of types, just like tensors\n",
        "bool_variable = tf.Variable([False, False, False, True])\n",
        "complex_variable = tf.Variable([5 + 4j, 6 + 1j])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "VQHwJ_Itoujf"
      },
      "source": [
        "A variable looks and acts like a tensor, and, in fact, is a data structure backed by a `tf.Tensor`.  Like tensors, they have a `dtype` and a shape, and can be exported to NumPy."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "GhNfPwCYpvlq"
      },
      "outputs": [],
      "source": [
        "print(\"Shape: \", my_variable.shape)\n",
        "print(\"DType: \", my_variable.dtype)\n",
        "print(\"As NumPy: \", my_variable.numpy())"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "eZmSBYViqDoU"
      },
      "source": [
        "Most tensor operations work on variables as expected, although variables cannot be reshaped."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "TrIaExVNp_LK"
      },
      "outputs": [],
      "source": [
        "print(\"A variable:\", my_variable)\n",
        "print(\"\\nViewed as a tensor:\", tf.convert_to_tensor(my_variable))\n",
        "print(\"\\nIndex of highest value:\", tf.argmax(my_variable))\n",
        "\n",
        "# This creates a new tensor; it does not reshape the variable.\n",
        "print(\"\\nCopying and reshaping: \", tf.reshape(my_variable, [1,4]))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "qbLCcG6Pc29Y"
      },
      "source": [
        "As noted above, variables are backed by tensors. You can reassign the tensor using `tf.Variable.assign`.  Calling `assign` does not (usually) allocate a new tensor; instead, the existing tensor's memory is reused."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "yeEpO309QbB2"
      },
      "outputs": [],
      "source": [
        "a = tf.Variable([2.0, 3.0])\n",
        "# This will keep the same dtype, float32\n",
        "a.assign([1, 2]) \n",
        "# Not allowed as it resizes the variable: \n",
        "try:\n",
        "  a.assign([1.0, 2.0, 3.0])\n",
        "except Exception as e:\n",
        "  print(f\"{type(e).__name__}: {e}\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "okeywjLdQ1tY"
      },
      "source": [
        "If you use a variable like a tensor in operations, you will usually operate on the backing tensor.  \n",
        "\n",
        "Creating new variables from existing variables duplicates the backing tensors. Two variables will not share the same memory."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "2CnfGc6ucbXc"
      },
      "outputs": [],
      "source": [
        "a = tf.Variable([2.0, 3.0])\n",
        "# Create b based on the value of a\n",
        "b = tf.Variable(a)\n",
        "a.assign([5, 6])\n",
        "\n",
        "# a and b are different\n",
        "print(a.numpy())\n",
        "print(b.numpy())\n",
        "\n",
        "# There are other versions of assign\n",
        "print(a.assign_add([2,3]).numpy())  # [7. 9.]\n",
        "print(a.assign_sub([7,9]).numpy())  # [0. 0.]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ZtzepotYUe7B"
      },
      "source": [
        "## Lifecycles, naming, and watching\n",
        "\n",
        "In Python-based TensorFlow, `tf.Variable` instance have the same lifecycle as other Python objects. When there are no references to a variable it is automatically deallocated.\n",
        "\n",
        "Variables can also be named which can help you track and debug them.  You can give two variables the same name."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "VBFbzKj8RaPf"
      },
      "outputs": [],
      "source": [
        "# Create a and b; they will have the same name but will be backed by\n",
        "# different tensors.\n",
        "a = tf.Variable(my_tensor, name=\"Mark\")\n",
        "# A new variable with the same name, but different value\n",
        "# Note that the scalar add is broadcast\n",
        "b = tf.Variable(my_tensor + 1, name=\"Mark\")\n",
        "\n",
        "# These are elementwise-unequal, despite having the same name\n",
        "print(a == b)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "789QikItVA_E"
      },
      "source": [
        "Variable names are preserved when saving and loading models. By default, variables in models will acquire unique variable names automatically, so you don't need to assign them yourself unless you want to.\n",
        "\n",
        "Although variables are important for differentiation, some variables will not need to be differentiated.  You can turn off gradients for a variable by setting `trainable` to false at creation. An example of a variable that would not need gradients is a training step counter."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "B5Sj1DqhbZvx"
      },
      "outputs": [],
      "source": [
        "step_counter = tf.Variable(1, trainable=False)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "DD_xfDLDTDNU"
      },
      "source": [
        "## Placing variables and tensors\n",
        "\n",
        "For better performance, TensorFlow will attempt to place tensors and variables on the fastest device compatible with its `dtype`.  This means most variables are placed on a GPU if one is available.\n",
        "\n",
        "However, you can override this.  In this snippet, place a float tensor and a variable on the CPU, even if a GPU is available.  By turning on device placement logging (see [Setup](#scrollTo=xZoJJ4vdvTrD)), you can see where the variable is placed. \n",
        "\n",
        "Note: Although manual placement works, using [distribution strategies](distributed_training) can be a more convenient and scalable way to optimize your computation.\n",
        "\n",
        "If you run this notebook on different backends with and without a GPU you will see different logging.  *Note that logging device placement must be turned on at the start of the session.*"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "2SjpD7wVUSBJ"
      },
      "outputs": [],
      "source": [
        "with tf.device('CPU:0'):\n",
        "\n",
        "  # Create some tensors\n",
        "  a = tf.Variable([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])\n",
        "  b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])\n",
        "  c = tf.matmul(a, b)\n",
        "\n",
        "print(c)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "PXbh-p2BXKcr"
      },
      "source": [
        "It's possible to set the location of a variable or tensor on one device and do the computation on another device.  This will introduce delay, as data needs to be copied between the devices.\n",
        "\n",
        "You might do this, however, if you had multiple GPU workers but only want one copy of the variables."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "dgWHN3QSfNiQ"
      },
      "outputs": [],
      "source": [
        "with tf.device('CPU:0'):\n",
        "  a = tf.Variable([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])\n",
        "  b = tf.Variable([[1.0, 2.0, 3.0]])\n",
        "\n",
        "with tf.device('GPU:0'):\n",
        "  # Element-wise multiply\n",
        "  k = a * b\n",
        "\n",
        "print(k)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "fksvRaqoYfay"
      },
      "source": [
        "Note: Because `tf.config.set_soft_device_placement` is turned on by default, even if you run this code on a device without a GPU, it will still run.  The multiplication step will happen on the CPU.\n",
        "\n",
        "For more on distributed training, see [our guide](distributed_training)."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "SzCkWlF2S4yo"
      },
      "source": [
        "## Next steps\n",
        "\n",
        "To understand how variables are typically used, see our guide on [automatic differentiation](autodiff.ipynb)."
      ]
    }
  ],
  "metadata": {
    "colab": {
      "collapsed_sections": [],
      "name": "variable.ipynb",
      "toc_visible": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
